package com.service.socket.process;

import java.util.List;

import com.service.bean.other.Frame;
import com.service.cache.CacheManager;
import com.service.other.ServerConfig;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.Packet;

public class PacketCreator {
	
	public static Packet getFrameBean(String deviceNo,String version){
		List<Frame> list = CacheManager.instance().getFrame(version);
		StringBuilder builder = new StringBuilder();
		builder.append(Conversion.bytesToHexString(version.getBytes()));
		builder.append("00");
		builder.append(Conversion.reversal(Conversion.padLeft(4, Integer.toHexString(ServerConfig.instance().frame_size), '0')));
		builder.append(Conversion.reversal(Conversion.padLeft(8, Integer.toHexString(list.size()), '0')));
		builder.append(Conversion.checkSum(builder.toString()));
		Packet bean = new Packet();
		bean.cmdType = "F0";
		bean.cmdWord = "56";
		bean.status = "02";
		bean.deviceNo = Conversion.padLeft(14, deviceNo, '0');
		bean.dataArea = builder.toString();
		bean.length = bean.getLength();
		bean.crcValue = bean.getCrcValue();
		return bean;
	}
	
	public static Packet getFrameBean(String deviceNo,String version,int index){
		List<Frame> list = CacheManager.instance().getFrame(version);
		System.out.println("文件总帧数"+list.size()+",当前请求第"+(index+1)+"帧");
		Frame frame = list.get(index);
		Packet bean = new Packet();
		bean.cmdType = "F0";
		bean.cmdWord = "58";
		bean.status = "02";
		bean.deviceNo = Conversion.padLeft(14, deviceNo, '0');
		bean.dataArea = frame.toString();
		bean.length = bean.getLength();
		bean.crcValue = bean.getCrcValue();
		return bean;
	}
	
}
